﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Smarly.Forum.Bussines.Model;
using Smarly.Web.Areas.StapleForum.Helpers;
using Smarly.Web.Controllers;
using Smarly.Web.Controllers.Base;

namespace Smarly.Web.Areas.StapleForum.Controllers
{
	public class ThreadController : BaseController
	{
		public ActionResult Create(string section)
		{
			ThreadModel model = new ThreadModel() { SectionHashTag = section }/*.GetCrumb()*/;
			return View(model);
		}

		[Authorize]
		[HttpPost]
		public ActionResult Create(ThreadModel model)
		{
			if (model.ModelState.IsValid)
			{
				model.Post(UserIdentity);
			}
			else
			{
//				model.GetCrumb();
			}
			
			if (IsAjax)
			{
				if (model.ModelState.IsValid)
				{
					return Json(new {url = Url.Action("Index", new {section = model.SectionHashTag, id = model.Id})});
				}
				else
				{
					return JsonValidationErrors();
				}
			}

			if (model.ModelState.IsValid)
				return RedirectToAction("Index", new { section = model.SectionHashTag, id = model.Id });
			else
				return View(model);
		}

		public ActionResult Index(string section, long id)
		{
			ThreadModel model = new ThreadModel() { SectionHashTag = section, Id = id}.Get(UserIdentity);
			return View(model);
		}

		[Authorize]
		[HttpPost]
		public ActionResult AddClarify(ThreadModel.ClarifyNote model, string section, long threadId)
		{
			if (ModelState.IsValid)
			{
				model.Post(UserIdentity);

				if (IsAjax)
				{
					return Json(new { });
/*
					if (model.isNew)
						return PartialView("_ClarifyPartial", model);
					return PartialView("_EmptyPartial");
*/
				}

				return RedirectToAction("Index", new {id = model.ThreadId, section = model.SectionHashTag});
			}
			else
			{
				if (IsAjax)
					return JsonValidationErrors();

				ThreadModel thread = new ThreadModel() { SectionHashTag = section, Id = threadId }.Get(UserIdentity);
				ViewBag.NewClarify = model;
				return View("Index", thread);
			}
		}

		[Authorize, HttpPost]
		public ActionResult AddNote(ThreadModel.ThreadNote model, string section)
		{
			if (ModelState.IsValid)
			{
				model.Post(UserIdentity);

				if (IsAjax)
				{
					return Json(new {});
/*
					if (model.isNew)
						return PartialView("_ThreadNotePartial", model);
					return PartialView("_EmptyPartial");
*/
				}

				return RedirectToAction("Index", new { id = model.ThreadId, section = model.Thread.SectionHashTag });
			}
			else
			{
				if (IsAjax)
					return JsonValidationErrors();

				ThreadModel thread = new ThreadModel() { SectionHashTag = section, Id = model.ThreadId }.Get(UserIdentity);
				ViewBag.NewNote = model;
				return View("Index", thread);
			}
		}

		[Authorize, HttpGet]
		public ActionResult EditNote(long id)
		{
			ThreadModel.ThreadNote model = new ThreadModel.ThreadNote{Id = id}.Get(UserIdentity);
			return View(model);
		}

		[Authorize, HttpPost]
		public ActionResult EditNote(ThreadModel.ThreadNote model)
		{
			if (ModelState.IsValid)
			{
				model.Put(UserIdentity);

				var url = Url.ThreadAction(model);

				if (IsAjax)
				{
					return Json(new { url = url });
				}

				return Redirect(url);
			}
			else
			{
				if (IsAjax)
					return JsonValidationErrors();

				throw new NotImplementedException();
			}
		}

/*		[Authorize, HttpGet]
		public ActionResult EditClarify(long id)
		{
			ThreadModel.ClarifyNote model = new ThreadModel.ClarifyNote { Id = id }.Get(UserIdentity);
			return View(model);
		}*/

		[Authorize, HttpPost]
		public ActionResult EditClarify(ThreadModel.ClarifyNote model)
		{
			if (ModelState.IsValid)
			{
				model.Put(UserIdentity);

				//var url = Url.ThreadAction(model);

				if (IsAjax)
				{
					return Json(new { });
				}

				throw new NotImplementedException();
			}
			else
			{
				if (IsAjax)
					return JsonValidationErrors();

				throw new NotImplementedException();
			}
		}


	}
}
